<?php

namespace Wechat\Controller;

use Think\Controller;


class TestController extends Controller {
  
  var $redirect_url = 'http://www.cmccwhwx.com/api/v1.0/wxjsapi?url=';
  
  public function index() {
    
    $url = U('Wechat/Test/index' , '' , TRUE , TRUE );
    
    $data['wx_config'] = json_decode( file_get_contents( $this->_get_wx_config($url) ) , TRUE);
    $data['share_data'] = array(
      'title' => '这是分享标题',
      'link'  => 'http://ryhd.socialpeck.com/wechat/cupid/index/1/1?user_token=test',
      'icon'  => U('Public/themes/wechat/cupid/images/share_icon.jpg' , '' , TRUE , TRUE ),
    );
    $this->assign($data);
    $this->display();
  }

  function _get_wx_config( $url ) {
    return $this->redirect_url . urlencode($url);
  }

  /**
   * 从api取用户信息
   * @param type $user_token
   */
  function _get_user_info_from_api($user_token) {
    if ( $user_token == 'test' ) {
      return array(
        'uid'         => 232824 ,
        'status'     => 1 ,
        'msg'        => 'ok' ,
        'phone'      => '15926212058' ,
        'nickname'   => 'Em!Ly_小爱' ,
        'headimgurl' => 'http://wx.qlogo.cn/mmopen/SYeWkon6C6IqS50Y2Ov4ft2fOcwQFXIP1iaia70q1ynZTwhOgCh73YYWYeZIJs0wmcUNKrDibia7usE1Cib7rJhZwtQ/96' ,
      ); 
    }
    
    $data = array(
      'appid' => self::APPID ,
      'user_token' => $user_token ,
    );
    
    $send_data = $this->_make_sign($data);
    $send_url = self::GET_USERINFO_URL . http_build_query($send_data);
    
    //echo $send_url ;    
    return $this->_http_get( $send_url );
  }
  
  /**
   * 签名
   * @param array $data
   * @return type
   */
  function _make_sign($data) {
    $data['token'] = self::TOKEN;
    ksort($data);
    $arr = array();
    foreach ( $data as $key => $val ) {
      $arr[] = "$key=" . $val ;
    }
    $string = implode('&', $arr);
    //echo $string . '<br>';
    $data['sign'] = sha1($string);
    unset( $data['token'] );
    return $data;
  }
  
  /**
   * curl get
   * @param type $url
   * @return type
   */
  function _http_get($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    //优化
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect: '));
    curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    $res = curl_exec($curl);
    curl_close($curl);

    //file_put_contents('./Public/tmp/cmcc.txt', date('Y-m-d H:i:s') . ' ' . $res . "\r\n" , FILE_APPEND );
    $ret = json_decode($res, TRUE);
    return $ret;
  }


  function _check_mer_info( $mer_id ) {
    if( empty($mer_id) ) {
      gameover('商户参数错误');
    }

    //取商户信息
    $Merchant = MerchantService::instance();
    $mer_data = $Merchant->get_by_id($mer_id);

    if ( empty( $mer_data ) ) {
      gameover('商户信息错误');
    }
    return $mer_data ;
  }

  function _check_event_info( $mer_id , $event_id ) {
    $WxEventCupid = WxEventCupidService::instance();
    $today = strtotime(date('Y-m-d'));
    $event_data = $WxEventCupid->get_by_id($event_id);
    if ( $mer_id != $event_data['mer_id'] ) {
      gameover('商户没有该活动');
    }
    
    if ( $today < strtotime($event_data['start_time']) ) {
      gameover('活动还未开始');
    }
    
    if ( $today > strtotime($event_data['end_time']) ) {
      gameover('活动已经结束');
    }
    return $event_data ;
  }
  
  
  function test() {
    $this->display();
  }
}
